package com.tarena.lbs.marketing.web.service.impl;

import com.tarena.lbs.marketing.web.repository.UserReceiveCouponRepository;
import com.tarena.lbs.marketing.web.service.UserReceiveCouponService;
import com.tarena.lbs.attach.api.AttachApi;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.basic.api.StoreApi;
import com.tarena.lbs.common.attach.enums.BusinessTypeEnum;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.common.security.utils.LbsSecurityContenxt;
import com.tarena.lbs.pojo.attach.dto.AttachQrDTO;
import com.tarena.lbs.pojo.attach.param.AttachQRParam;
import com.tarena.lbs.pojo.basic.vo.StoreVO;
import com.tarena.lbs.pojo.marketing.bo.UserReceiveCouponBO;
import com.tarena.lbs.pojo.marketing.param.UpdateParam;
import com.tarena.lbs.pojo.marketing.param.UserReceiveCouponParam;
import com.tarena.lbs.pojo.marketing.query.UserCouponCodeQuery;
import com.tarena.lbs.pojo.marketing.query.UserCouponQuery;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class UserReceiveCouponServiceImpl implements UserReceiveCouponService {

    @Resource
    private StoreApi storeRpcApi;

    @Resource
    private AttachApi attachApi;

    @Resource
    private UserReceiveCouponRepository userReceiveCouponRepository;

    @Override
    public BasePageList<UserReceiveCouponBO> userList(UserCouponQuery couponQuery) {
        return userReceiveCouponRepository.userList(couponQuery);
    }

    @Override
    public List<UserReceiveCouponBO> userCouponList(Integer userId,Integer couponId) {
        return userReceiveCouponRepository.userCouponList(userId,couponId);
    }

    @Override
    public UserReceiveCouponBO userCouponDetail(UserCouponCodeQuery query) {
        UserPrinciple loginUser = LbsSecurityContenxt.getLoginToken();
        if (loginUser != null) {
            query.setUserId(loginUser.getId());
        }
        UserReceiveCouponBO userReceiveCouponBO = userReceiveCouponRepository.userCouponDetail(query);
        //获取店铺名字
        StoreVO storeVO = storeRpcApi.getStoreById(userReceiveCouponBO.getShopId());
        userReceiveCouponBO.setShopName(storeVO == null ? "" : storeVO.getStoreName());
        AttachQRParam attachQRParam = new AttachQRParam(userReceiveCouponBO.getCouponCode(),userReceiveCouponBO.getId(), BusinessTypeEnum.COUPON_CODE.getType());
        AttachQrDTO attachQrDTO = attachApi.generateQrCode(attachQRParam);
        userReceiveCouponBO.setCouponUrl(attachQrDTO == null ? "" : attachQrDTO.getUrl());
        return userReceiveCouponBO;
    }

    @Override
    public void insert(UserReceiveCouponParam param) {
        userReceiveCouponRepository.insert(param);
    }

    @Override
    public void update(UserReceiveCouponParam param) {
        userReceiveCouponRepository.update(param);
    }

    @Override
    public void updateStatus(UpdateParam param) {
        userReceiveCouponRepository.updateStatus(param);
    }
}
